const itemList = {}
const promotionList = {}

const testParse1 = () => {
    const a = ["ITEM0013 x 4", "ITEM0022 x 1"]
    const expect = {
        'ITEM0013': {
            id: 'ITEM0013',
            count: 4
        },
        'ITEM0022': {
            id: 'ITEM0022',
            count: 1
        }
    }
    const output = Order.parseInput(a)
    ensureEqual(JSON.stringify(expect), JSON.stringify(output), 'testParse1 error')
    dividerLog()
}

const testParse2 = () => {
    const a = ["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]
    const expect = {
        'ITEM0001': {
            id: 'ITEM0001',
            count: 1
        },
        'ITEM0013': {
            id: 'ITEM0013',
            count: 2
        },
        'ITEM0022': {
            id: 'ITEM0022',
            count: 1
        },
    }
    const output = Order.parseInput(a)
    ensureEqual(JSON.stringify(expect), JSON.stringify(output), 'testParse2 error')
    dividerLog()
}

const testParse3 = () => {
    const a = ["ITEM0013 x 4"]
    const expect = {
        'ITEM0013': {
            id: 'ITEM0013',
            count: 4
        },
    }
    const output = Order.parseInput(a)
    ensureEqual(JSON.stringify(expect), JSON.stringify(output), 'testParse3 error')
    dividerLog()
}

// 添加指定菜品半价优惠
const testPromotionAdd = () => {
    log('测试-添加指定菜品半价优惠')
    const promotion1 = new Promotion('promotion1', 'halfPrice', '指定菜品半价', Rule.halfPrice)
    Promotion.addPromotion(promotionList, promotion1)
    log('testPromotionAdd', promotionList)
    dividerLog()
}

// 添加满减优惠
const testPromotionAdd2 = () => {
    log('测试-添加满减优惠')
    const promotion2 = new Promotion('promotion2', 'fullDiscount', '满30减6', Rule.fullDiscount)
    Promotion.addPromotion(promotionList, promotion2)
    log('testPromotionAdd2', promotionList)
    dividerLog()
}

// 修改规则
const testPromotionChangeRule = () => {
    log('测试-修改规则')
    const promotion3 = new Promotion('promotion3', 'fullDiscountTest', '测试满30减6', Rule.fullDiscount)
    promotion3.changeRule(Rule.newRule)
    Promotion.addPromotion(promotionList, promotion3)
    log('testPromotionChangeRule', promotionList)
    dividerLog()
}
// 测试 change 方法，修改 name
const testPromotionChange = () => {
    log('测试-change 方法，修改 name')
    const promotion4 = new Promotion('promotion4', 'testDiscount', '测试满30减6', Rule.fullDiscount)
    promotion4.change('promotionName', '测试大减价')
    Promotion.addPromotion(promotionList, promotion4)
    log('testPromotionChange', promotionList)
    dividerLog()
}


// 添加三种商品
const testItemAdd = () => {
    log('测试-添加三种商品')
    const item1 = new Item('ITEM0001', '黄焖鸡', 18)
    const item2 = new Item('ITEM0013', '肉夹馍', 6)
    const item3 = new Item('ITEM0022', '凉皮', 8)
    const item4 = new Item('ITEM5001', '测试商品', 3)
    Item.addItem(itemList, item1)
    log('testItem1-1', itemList)
    Item.addItem(itemList, item2)
    log('testItem1-2', itemList)
    Item.addItem(itemList, item3)
    log('testItem1-3', itemList)
    Item.addItem(itemList, item4)
    log('testItem1-4', itemList)
    dividerLog()
}

// 给黄焖鸡，肉夹馍，凉皮添加满减优惠
const testItemAddPromotion = () => {
    log('测试-给黄焖鸡，肉夹馍，凉皮添加满减优惠')
    log('testItemAddPromotion', itemList)
    itemList['ITEM0001'].itemAddPromotion('promotion2', 'fullDiscount', promotionList)
    itemList['ITEM0013'].itemAddPromotion('promotion2', 'fullDiscount', promotionList)
    itemList['ITEM0022'].itemAddPromotion('promotion2', 'fullDiscount', promotionList)
    log('testItemAddPromotion end', itemList)
    dividerLog()
}

// 给黄焖鸡凉皮添加半价优惠
const testItemAddPromotion2 = () => {
    log('测试-给黄焖鸡凉皮添加半价优惠')
    log('testItemAddPromotion2', itemList)
    itemList['ITEM0001'].itemAddPromotion('promotion1', 'halfPrice', promotionList)
    itemList['ITEM0022'].itemAddPromotion('promotion1', 'halfPrice', promotionList)
    log('testItemAddPromotion2 end', itemList)
    dividerLog()
}

// 同时添加多个优惠
const testItemAddPromotionList = () => {
    log('测试-同时添加多个优惠')
    const item4 = new Item('ITEM5000', '测试商品1', 4, {})
    Item.addItem(itemList, item4)
    log('testItemAddPromotionList', itemList)
    itemList['ITEM5000'].itemAddPromotionList([['promotion1', 'halfPrice'], ['promotion2', 'fullDiscount']], promotionList)
    itemList['ITEM5001'].itemAddPromotionList([['promotion4', 'testDiscount']], promotionList)
    log('testItemAddPromotionList end', itemList)
    dividerLog()
}


const testClearItemPromotion = () => {
    log('测试-清除ITEM5000的优惠')
    log('testClearItemPromotion', itemList)
    itemList['ITEM5000'].clearItemPromotion()
    log('testClearItemPromotion end', itemList)
    dividerLog()
}

const testBestCharge1 = () => {
    const a = ["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]
    const expect = `
            ============= 订餐明细 =============
            黄焖鸡 x 1 = 18元
            肉夹馍 x 2 = 12元
            凉皮 x 1 = 8元
            -----------------------------------
            使用优惠:
            指定菜品半价(黄焖鸡，凉皮)，省13元
            -----------------------------------
            总计：25元
            ===================================
    `
    const order1 = new Order(a)
    const output = order1.bestCharge(a, itemList, promotionList)
    log(output)
    ensureEqual(expect, output, 'testBestCharge1 error')
    dividerLog()
}

const testBestCharge2 = () => {
    const a = ["ITEM0013 x 4", "ITEM0022 x 1"]
    const expect = `
            ============= 订餐明细 =============
            肉夹馍 x 4 = 24元
            凉皮 x 1 = 8元
            -----------------------------------
            使用优惠:
            满30减6元，省6元
            -----------------------------------
            总计：26元
            ===================================
    `
    const order2 = new Order(a)
    const output = order2.bestCharge(a, itemList, promotionList)
    log(output)
    ensureEqual(expect, output, 'testBestCharge2 error')
    dividerLog()
}

const testBestCharge3 = () => {
    const a = ["ITEM0013 x 4"]
    const expect = `
            ============= 订餐明细 =============
            肉夹馍 x 4 = 24元
            -----------------------------------
            总计：24元
            ===================================
    `
    const order3 = new Order(a)
    const output = order3.bestCharge(a, itemList, promotionList)
    log(output)
    ensureEqual(expect, output, 'testBestCharge3 error')
    dividerLog()
}

const testBestCharge4 = () => {
    const a = ["ITEM0013 x 6"]
    const expect = `
            ============= 订餐明细 =============
            肉夹馍 x 6 = 36元
            -----------------------------------
            使用优惠:
            满30减6元，省6元
            -----------------------------------
            总计：30元
            ===================================
    `
    const order4 = new Order(a)
    const output = order4.bestCharge(a, itemList, promotionList)
    log(output)
    ensureEqual(expect, output, 'testBestCharge4 error')
    dividerLog()
}

const testBestCharge5 = () => {
    const a = ["ITEM0001 x 1", "ITEM0022 x 1"]
    const expect = `
            ============= 订餐明细 =============
            黄焖鸡 x 1 = 18元
            凉皮 x 1 = 8元
            -----------------------------------
            使用优惠:
            指定菜品半价(黄焖鸡，凉皮)，省13元
            -----------------------------------
            总计：13元
            ===================================
    `
    const order5 = new Order(a)
    const output = order5.bestCharge(a, itemList, promotionList)
    log(output)
    ensureEqual(expect, output, 'testBestCharge4 error')
    dividerLog()
}
